﻿using System;
using System.Text;
using System.Web.UI;
using System.ComponentModel;

namespace Lswweb.Controls
{
    public class Panel : SkinnedWebControl
    {
        private ITemplate headTemplate=null;
        private ITemplate contentTemplate=null;
        private Control headPanel;
        private Control contentPanel;
        private string headCss;
        private string contentCss;
        private string headStyle;
        private string contentStyle;

        protected void RenderScriptBlock(HtmlTextWriter writer)
        {
            StringBuilder scriptBuilder = new StringBuilder();
            scriptBuilder.Append("<script language='javascript' type='text/javascript'>");
            scriptBuilder.Append("$(function(){");

            if (!string.IsNullOrEmpty(headCss))
                scriptBuilder.Append(string.Format("$(\"#{0}\").addClass(\"{1}\")", headPanel.ClientID, headCss));

            if (!string.IsNullOrEmpty(contentCss))
                scriptBuilder.Append(string.Format("$(\"#{0}\").addClass(\"{1}\")", contentPanel.ClientID, contentCss));

            if (!string.IsNullOrEmpty(headStyle))
                scriptBuilder.Append(string.Format("$(\"#{0}\").attr(\"style\",\"{1}\")", headPanel.ClientID, headStyle));

            if (!string.IsNullOrEmpty(contentStyle))
                scriptBuilder.Append(string.Format("$(\"#{0}\").attr(\"style\",\"{1}\")", contentPanel.ClientID, contentStyle));

            scriptBuilder.Append("});");
            scriptBuilder.Append("</script>");

            writer.Write(scriptBuilder.ToString());
            
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);

            //只能在这里输出
            this.RenderScriptBlock(writer);
        }

        protected override void InitializeSkin(Control skin)
        {
            headPanel = skin.FindControl("headPanel");
            contentPanel = skin.FindControl("contentPanel");

            if (HeadTemplate != null && headPanel != null)
                HeadTemplate.InstantiateIn(headPanel);

            if (ContentTemplate != null && contentPanel != null)
                ContentTemplate.InstantiateIn(contentPanel);

        }

        #region 公共属性

        [Browsable(false)]
        [TemplateContainer(typeof(Lswweb.Controls.Panel))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ITemplate HeadTemplate
        {
            get { return headTemplate; }
            set { headTemplate = value; }
        }

        [Browsable(false)]
        [TemplateContainer(typeof(Lswweb.Controls.Panel))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ITemplate ContentTemplate
        {
            get { return contentTemplate; }
            set { contentTemplate = value; }
        }

        public string HeadCss
        {
            get { return headCss; }
            set { headCss = value; }
        }

        public string ContentCss
        {
            get { return contentCss; }
            set { contentCss = value; }
        }

        public string HeadStyle
        {
            get { return headStyle; }
            set { headStyle = value; }
        }

        public string ContentStyle
        {
            get { return contentStyle; }
            set { contentStyle = value; }
        }

        #endregion

        public override Control FindControl(string id)
        {
            EnsureChildControls();

            Control control=base.FindControl(id);
            if (control == null && headPanel != null)
                control = headPanel.FindControl(id);

            if (control == null && contentPanel != null)
                control = contentPanel.FindControl(id);

            return control;
        }

        protected override string DefaultSkinFilename
        {
            get { return "panel/panel.ascx"; }
        }
    }
}
